package com.webcamtracker.tracker.model;


import com.webcamtracker.image.color.hsv.HSVColor;
import com.webcamtracker.utils.Hit;
import com.webcamtracker.utils.Hits;

import java.awt.*;
import java.util.logging.Logger;

public class RectangleTrackerModel extends AbstractTrackerModel {

    private static Logger loger = Logger.getAnonymousLogger();
    private Hits hits;
    private Rectangle figure;

    public RectangleTrackerModel(ModelElement child, Hits hits) {
        this.hits = hits;
        figure = new Rectangle(child);
    }

    public RectangleTrackerModel(ModelElement child, Hits hits, String name) {
        this.hits = hits;
        figure = new Rectangle(child);
        setName(name);
    }

    public void add(ModelElement element) {
        figure.add(element);
        int x = (int) element.getX();
        int y = (int) element.getY();
        int maxX = (int) element.getMaxX();
        int maxY = (int) element.getMaxY();
        while (x < maxX) {
            while (y < maxY) {
                HSVColor color = (HSVColor) element.getColorAt(x, y);
                Hit hit = new Hit(color);
                int index = hits.indexOf(hit);
                if (index > -1) {
                    hits.get(index).hit(color);
                    hits.newElement();
                }
                y++;
            }
            x++;
        }

    }


    public void setHits(Hits hits) {
        this.hits = hits;
    }

    public Hits getHits() {
        return hits;
    }

    public boolean contains(TrackerModel model) {
        return figure.contains(model.getX(), model.getY(), model.getWidth(), model.getHeight());
    }

    public boolean contains(int x, int y) {
        return figure.contains(x, y);
    }

    public int getWidth() {
        return (int) figure.getWidth();
    }


    public int getHeight() {
        return (int) figure.getHeight();
    }


    public int getX() {
        return (int) figure.getX();
    }

    public int getY() {
        return (int) figure.getY();
    }

    public void paint(Graphics g) {

        g.setColor(Color.RED);
        g.drawRect(figure.x, figure.y, figure.width, figure.height);
        g.setColor(Color.GREEN);
        g.drawString(getName(), figure.x, figure.y);

    }
}
